package com.exam.scjp5.objectives.section_1;

public class ExampleOverriding
{
    public static void main(String args[])
    {
        Animal a = new Dog();
        Dog d = new Dog();
        
        d.method8(1, 1); //OK.
       
        
        int i = new Integer('a');
        Integer inte = new Integer(161284);
        System.out.println(inte.toOctalString(161284));
        
        // ERROR! Reference type is Animal and
        // Animal's method8 has checked exception.
        //a.method8(1, 1);
        a.method9(null);
        d.method9(null);
        
        
        Animal animal = new Dog();
        Dog dog = (Dog) animal;
        animal.method10("");dog.method10("test");
        animal.method11("");dog.method11("test");
    }
}

class Animal
{
    // Can be overridden by any subclass.
    public void method1(int i){}
    
    // Can be overridden by any subclass and 
    // also any subclass in same package.
    protected void method2(int i){} 
    
    // Can be overridden by any subclass in same package.
    void method3(int i){} 
    
    // Can't be overridden.
    final public void method4(){} 
    
    // Can't be overridden but can be redefined.
    static public void method5(){} 
    static public void method6(){}
    
    public Animal method7(int a,int b){ return new Animal();}
    public Animal method8(int a,int b) throws Exception
    { return new Animal();}
    public Integer method9(Number o)
    {
        System.out.println("Animal.method9(Number o)");return null;
    }
    
    public void method10(String... args)
    {
    	System.out.println("Animal.method10(String... args)");
    }
    
    public void method11(String args)
    {
    	System.out.println("Dog.method10(String... args)");
    }
    
    public Object method12(){ return null; }
}

class Dog extends Animal
{
    // Overriding method MUST have same arguments as overridden method. 
    // Or else, it is just OVERLOADING.
    public void method1() // HERE! Overloading
    { 
        // new definition    
    }
    
    //public void method4(){} // ERROR! Can't overwrite. It's FINAL.
    static public void method5(){} // OK! Just redefining NOT overriding
    
    // static method of superclass can't be overridden 
    // by subclass's instance methods.
    // It will be compiler error.
    //public void method6(){}
    
    // Valid overriding with covariant return type
    public Dog method7(int a,int b){ return new Dog();}
    public Dog method8(int a,int b){ return new Dog();}
    public Integer method9(Number o)
    {
    	Integer obj11 = new Integer(1);
    	Integer nn = (Integer)(Object)obj11;
        System.out.println("Dog.method9(Number o)");return null;
        
    }
    
    public void method9(Integer o)
    {
        System.out.println("Dog.method9(Integer o)");
    }
    
    public void method10(String args)
    {
    	System.out.println("Dog.method10(String args)");
    }
    
    public void method11(String args)
    {
    	System.out.println("Dog.method10(String... args)");
    }
    
    public String method12(){ return null; }
}